package com.coupons.business.merchant.dao;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.support.SqlLobValue;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.support.lob.DefaultLobHandler;
import org.springframework.jdbc.support.lob.LobHandler;

import com.coupons.business.common.exception.BaseCouponsAppException;
import com.coupons.business.common.vo.Merchant;
import com.coupons.business.common.vo.decorator.MerchantRowMapper;
import com.coupons.business.util.BaseUtilDAO;
import java.sql.Types;
public class MerchantDAO extends BaseUtilDAO implements IMerchantDAO {

	
	/*
	 * (non-Javadoc)
	 *  @see com.coupons.business.merchant.dao.IMerchantDAO#createMerchant(com.coupons.business.common.vo.Merchant)
	 *  
	 *  INSERT INTO MERCHANT(MRCHNT_ID,MRCHNT_NAME,PARTNER_ID,DESCRIPTION,
	 *	STATUS,LCSN_END_DT,RANK,MERCHANT_OFFER_HEADER,MERCHANT_SITE_URL,
	 *	MERCHANT_EMBLEM,MERCHANT_ASSOCIATION_ID,LAST_UDT_USER,
	 *	CRTN_DT,LAST_UPDT_DT) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,SYSDATE,SYSDATE)
	 */
	public void createMerchant(Merchant objaMerchant)
			throws BaseCouponsAppException 
	{
		int merchantId= getSequenceNextValue(SEQUENCE_NAME);
		Object objParams[] = new Object[] { 
				new Integer(merchantId),
				objaMerchant.getMerchantName(),
				new Integer(objaMerchant.getPartnerId()),				
				objaMerchant.getDescription(),
				objaMerchant.getStatus(),
				objaMerchant.getLicenseEndDate(),
				objaMerchant.getRank(),
				objaMerchant.getMerchantOfferHeader(),
				objaMerchant.getMerchantSiteURL(),
				objaMerchant.getMerchantEmblem(),
				objaMerchant.getMerchantAssociationId(),				
				objaMerchant.getLastUpdatedUser(),				
				};		
		int rtnVal = this.saveOrUpdateOrDelete(MERCHANT_INSERT_STATEMENT.toString(), objParams);
		objaMerchant.setMerchantId(merchantId);		
		System.out.println("executed dao");
		if(rtnVal <= 0) {			
			System.out.println("Unable to insert record into Merchant table");
			return;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#updateMerchant(com.coupons.business.common.vo.Merchant)
	 * 
	 *  UPDATE MERCHANT SET MERCHANT_NAME = ?, DESCRIPTION = ?,
	 *	STATUS=?, LCSN_END_DT=?, RANK=?, MERCHANT_OFFER_HEADER=?, MERCHANT_SITE_URL=?,
	 *	MERCHANT_EMBLEM=?,MERCHANT_ASSOCIATION_ID=?,
	 *	LAST_UPDT_USER=?, LAST_UPDT_DT=SYSDATE WHERE MERCHANT_ID = ?
	 */
	public void updateMerchant(Merchant objaMerchant)
			throws BaseCouponsAppException 
	{
		Object objParams[] = new Object[] {				
				objaMerchant.getMerchantName(),								
				objaMerchant.getDescription(),
				objaMerchant.getStatus(),
				objaMerchant.getLicenseEndDate(),
				objaMerchant.getRank(),
				objaMerchant.getMerchantOfferHeader(),
				objaMerchant.getMerchantSiteURL(),
				objaMerchant.getMerchantEmblem(),
				objaMerchant.getMerchantAssociationId(),				
				objaMerchant.getLastUpdatedUser(),
				objaMerchant.getMerchantId()
				};
		int rtnVal = this.saveOrUpdateOrDelete(MERCHANT_UPDATE_STATEMENT.toString(), objParams);
		System.out.println("executed dao");
		if(rtnVal <= 0) {			
			System.out.println("Unable to insert record into Merchant table");
			return;
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getMerchant(int)
	 * SELECT * FROM PARTNER where MERCHANT_ID = ?
	 */
	public Merchant getMerchant(int merchantId) throws BaseCouponsAppException 
	{
		try {
			Object objParams[] = new Object[]{new Integer(merchantId)};
			List<Object> list = this.findRecords(GET_MERCHANT_STATEMENT, objParams,new MerchantRowMapper());
			
			if (null!=list && list.size()>0){				
					return (Merchant)list.get(0);
			}
			else
				return null;
		} catch (BaseCouponsAppException e) {			
			e.printStackTrace();
			throw e;
		}		
	}

	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getAllMerchants()
	 * SELECT * FROM MERCHANT
	 */
	public List<Merchant> getAllMerchants() throws BaseCouponsAppException 
	{
		try {
			List list = this.findRecords(GET_ALL_MERCHANTS_STATEMENT, new MerchantRowMapper());
			return list;
		} catch (BaseCouponsAppException e) {			
			e.printStackTrace();
			throw e;
			}
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#getMerchant(int)
	 * SELECT * FROM PARTNER where MERCHANT Name = ?
	 */
	public Merchant getMerchant(String straMerchantName) throws BaseCouponsAppException 
	{
		try {
			Object objParams[] = new Object[]{straMerchantName};
			List<Object> list = this.findRecords(GET_MERCHANT__BY_NAME_STATEMENT, objParams,new MerchantRowMapper());
			
			if (null!=list && list.size()>0){				
					return (Merchant)list.get(0);
			}
			else
				return null;
		} catch (BaseCouponsAppException e) {			
			e.printStackTrace();
			throw e;
		}		
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.coupons.business.merchant.dao.IMerchantDAO#createMerchantV1(com.coupons.business.common.vo.Merchant)
	 * ,,,, ,,,
	 * ,, 
	 * ,,,,	 * 
	 */	
	public void createMerchantV1(Merchant objaMerchant) throws BaseCouponsAppException
	{
		SqlUpdate objlSqlUpdate = new SqlUpdate();
		objlSqlUpdate.setDataSource(getDataSource());
		objlSqlUpdate.setSql(MERCHANT_INSERT_STATEMENT);
		System.out.println(MERCHANT_INSERT_STATEMENT);
		int merchantId= getSequenceNextValue(SEQUENCE_NAME);
		try{
				if (objaMerchant != null )
				{		
					objlSqlUpdate.declareParameter(new SqlParameter(Types.BIGINT));// ID
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//MRCHNT_NAME
					objlSqlUpdate.declareParameter(new SqlParameter(Types.BIGINT));//PARTNER_ID
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//DESCRIPTION
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//STATUS
					objlSqlUpdate.declareParameter(new SqlParameter(Types.DATE));//LCSN_END_DT
					objlSqlUpdate.declareParameter(new SqlParameter(Types.SMALLINT));//RANK
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//MERCHANT_OFFER_HEADER
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//MERCHANT_SITE_URL
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//MERCHANT_EMBLEM
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//MERCHANT_ASSOCIATION_ID
					objlSqlUpdate.declareParameter(new SqlParameter(Types.VARCHAR));//LAST_UDT_USER
					//objlSqlUpdate.declareParameter(new SqlParameter(Types.BLOB));//EMBLEm_DATA
//					objlSqlUpdate.declareParameter(new SqlParameter(Types.DATE));//CRTN_DT
//					objlSqlUpdate.declareParameter(new SqlParameter(Types.DATE));//LAST_UPDT_DT					
					
					objlSqlUpdate.compile();
			
					Object objParams[] = new Object[] { 
							new Integer(merchantId),
							objaMerchant.getMerchantName(),
							new Integer(objaMerchant.getPartnerId()),				
							objaMerchant.getDescription(),
							objaMerchant.getStatus(),
							objaMerchant.getLicenseEndDate(),
							new Integer(objaMerchant.getRank()),
							objaMerchant.getMerchantOfferHeader(),
							objaMerchant.getMerchantSiteURL(),
							objaMerchant.getMerchantEmblem(),
							objaMerchant.getMerchantAssociationId(),				
							objaMerchant.getLastUpdatedUser(),
							//null
							};		
			
					// lobHandler and sqlLobValue are used to handle clob data types
					//LobHandler lobHandler = new DefaultLobHandler();
					//objParams[12] = new SqlLobValue(objaMerchant.getEmblemData(), lobHandler);
					objlSqlUpdate.update(objParams);			
				}
		}catch (DataAccessException objaAccessException) 
		{
			throw objaAccessException;
		}
	}
	public void updateMerchantV1 (Merchant objaMerchant) throws BaseCouponsAppException{}
	
	public void saveMerchant()throws BaseCouponsAppException
	{
		
		
	}
	
}
